package com.whcoding.jwt.token.controller;

import com.whcoding.jwt.token.annotation.TokenAuthentication;
import com.whcoding.jwt.token.constant.AuthLevelEnum;
import com.whcoding.jwt.token.entity.OrmUserVO;
import com.whcoding.jwt.token.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Manager
 */
@RestController
@RequestMapping("ormUser")
public class OrmUserController {

	@Autowired
	private JwtUtils jwtUtils;

	@PostMapping("/login")
	public Map<String, Object> login(@RequestBody OrmUserVO user) {

		Map<String, Object> map = new HashMap<>(16);
		user.setPassword("123456");
		user.setUserName("user_1");

		String username = user.getUserName();
		String password = user.getPassword();
		// 省略 账号密码验证
		// 验证成功后发送token
		Map<String, Object> tokenMap = jwtUtils.parseJwtTokenPlus("111");
		String token = tokenMap.get("token").toString();
		if (token != null) {
			map.put("code", "200");
			map.put("message", "认证成功");
			map.put("token", token);
			return map;
		}
		map.put("code", "403");
		map.put("message", "认证失败");
		return map;
	}

	@TokenAuthentication(authLevel = AuthLevelEnum.NO_AUTH)
	@GetMapping(value = "/api/test")
	public String get() {
		System.out.println("执行了get请求");
		return "success";
	}
}

